package com.h2cloud.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h2cloud.entity.BaseResp;
import com.h2cloud.entity.Blog;
import com.h2cloud.entity.UserInfo;
import com.h2cloud.service.BlogService;
import com.h2cloud.service.UserInfoService;
import com.h2cloud.utils.JWTUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Blog)表控制层
 *
 * @author makejava
 * @since 2020-09-21 18:45:33
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private BlogService Service;

    @Resource
    private UserInfoService userInfoService;


    @GetMapping("test")
    public String test() {
        return "test springboot";
    }

    /**
     * 通过主键查询博客信息
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public Object selectOne(@PathVariable("id") int id) {
        System.out.println("@GetMapping(\"/{id}\")");


        BaseResp baseResp = new BaseResp();
        baseResp.setCode(200);
        baseResp.setMsg("请求成功");

        Blog blogInfo = Service.queryById(id);
        UserInfo authorInfo = userInfoService.queryById(blogInfo.getUserid());

        Map<String, Object> map = new HashMap<>();
        map.put("blogInfo", blogInfo);
        map.put("authorPic", authorInfo.getPic());

        baseResp.setData(map);

        return baseResp;
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @return
     */
    @GetMapping("/page/{pageNum}")
    public Object userList(@PathVariable("pageNum") int pageNum) {
        System.out.println("/page/{pageNum}");

        PageHelper.startPage(pageNum, 12);
        List<Blog> users = Service.selectAll();
        PageInfo<Blog> pageInfo = new PageInfo<Blog>(users);

        BaseResp baseResp = new BaseResp();
        baseResp.setCode(200);
        baseResp.setMsg("请求成功");
        baseResp.setData(pageInfo);

        return baseResp;
    }

    /**
     * 保存 or 修改 blog
     *
     * @param blog
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Object save(@RequestBody Blog blog, HttpServletRequest request) {
        System.out.println("/save");

        String token = request.getHeader("Authorization");
        System.out.println(token);
        String userid = JWTUtil.getToken(token).getClaim("id").asString();
        blog.setUserid(Integer.parseInt(userid));

        BaseResp baseResp = new BaseResp();

        if (blog.getId() == null) {
            baseResp.setMsg("保存博客");
            blog.setTime(new Timestamp(System.currentTimeMillis()));
            Service.insert(blog);

        } else {

            baseResp.setMsg("修改博客");
            Service.update(blog);
        }

        return baseResp;
    }

    /**
     * 获取用户的文章 and 草稿
     *
     * @param request
     * @return
     */
    @GetMapping("/getUserBlogs")
    @ResponseBody
    public Object getUserBlogs(HttpServletRequest request) {
        System.out.println("getUserBlogs:");

        String token = request.getHeader("Authorization");
        System.out.println(token);
        String userid = JWTUtil.getToken(token).getClaim("id").asString();

        Blog blog = new Blog();
        blog.setUserid(Integer.parseInt(userid));
        System.out.println(blog.toString());

        blog.setStatus(1);//文章
        List blogList = Service.queryAll(blog);

        blog.setStatus(0);//草稿
        List draftList = Service.queryAll(blog);

        List AllList = new ArrayList();
        AllList.add(blogList);
        AllList.add(draftList);

        BaseResp baseResp = new BaseResp();
        baseResp.setCode(200);
        baseResp.setMsg("已获取文章和草稿");
        baseResp.setData(AllList);

        return baseResp;
    }

    /**
     * 判断 blog 是否属于这个用户
     *
     * @param id
     * @param
     * @return
     */
    @GetMapping("/thisUserHasBlog/{id}")
    public Object thisUserHasBlog(@PathVariable("id") int id, HttpServletRequest request) {

        String token = request.getHeader("Authorization");
        System.out.println(token);
        String userid = JWTUtil.getToken(token).getClaim("id").asString();

        System.out.println("thisUserHasBlog: " + id + " " + userid);

        Blog blogInfo = Service.thisUserHasBlog(id, Integer.parseInt(userid));

        BaseResp baseResp = new BaseResp();
        baseResp.setCode(200);
        baseResp.setData(blogInfo);
        baseResp.setMsg("判断 blog 是否属于这个用户");

        return baseResp;
    }

    /**
     * 删除博客
     *
     * @param blogID
     * @return
     */
    @GetMapping("/delBLog")
    public Object delBLog(int blogID) {
        Service.deleteById(blogID);
        return "删除成功！";
    }
}
